package ar.com.coonocer.joy.eclipseplugin.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

//TODO terminar e invocar
//TODO Pedir settings del template seleccionado y generar fichero
public class TransformDialog extends Dialog {
	boolean ok;
	String templateGroup;
	String templateName;

	public TransformDialog(Shell parent) {
		super(parent);
	}

	public void open() {
		Shell parent = getParent();
		final Shell shell = new Shell(parent, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setText("J-OY Plugin - Transform");
		shell.setLayout(new GridLayout(2, true));
		//
		Label templateGroupLabel = new Label(shell, SWT.NULL);
		templateGroupLabel.setText("Template Group:");
		//
		final Text templateGroupText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		//
		Label templateNameLabel = new Label(shell, SWT.NULL);
		templateNameLabel.setText("Template Name:");
		//
		final Text templateNameText = new Text(shell, SWT.SINGLE | SWT.BORDER);
		//
		final Button buttonOK = new Button(shell, SWT.PUSH);
		buttonOK.setText("Ok");
		buttonOK.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		Button buttonCancel = new Button(shell, SWT.PUSH);
		buttonCancel.setText("Cancel");
		//
//		templateGroupText.addListener(SWT.Modify, new Listener() {
//			public void handleEvent(Event event) {
//				//
//			}
//		});

		buttonOK.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				ok=true;
				templateGroup=templateGroupText.getText();
				templateName=templateNameText.getText();
				//
				shell.dispose();
			}
		});

		buttonCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				ok=false;
				//
				shell.dispose();
			}
		});

		shell.addListener(SWT.Traverse, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.TRAVERSE_ESCAPE)
					event.doit = false;
			}
		});

		templateGroupText.setText("");
		//
		shell.pack();
		shell.open();
		//
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public static void main(String[] args) {
		Shell shell = new Shell();
		TransformDialog dialog = new TransformDialog(shell);
		dialog.open();
		System.out.println(dialog.ok);
	}
}
